<?php

class ConfigAction extends PublicAction
{
    public function index()
    {
        $configModel = M('Config');
        $groupId = I('get.groupid', 1, 'intval');
        $map = array(
            'groupid' => $groupId,
            'lang' => LANG_ID
        );
        $config = $configModel->where($map)->order('listorder asc')->select();

        $this->assign('config', $config);

        $this->assign('groupId', $groupId);

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $this->display();
    }

    public function save()
    {
        $config_db = M('Config');
        $where = "";

        $where .= " AND lang=" . LANG_ID;

        $sta = false;
        foreach ($_POST as $key => $value) {
            $data['value'] = $value;
            $f = $config_db->where("varname='" . $key . "'" . $where)->save($data);
            if ($f) {
                $sta = true;
            }
        }
        saveCache('Config');

        if ($sta) {
            $this->success('保存成功!');
        } else {
            $this->error('没有发生更改!');
        }
    }
    
    



   //自定义变量管理
    public function lists()
    {
        $config_db = M('Config');
        $map = array('lang' => LANG_ID);
        $count = $config_db->where($map)->count('id');

        if ($count) {
            $listRows = I('listRows', C('PAGE_LISTROWS'), 'intval');
            import("@.ORG.Page");
            //创建分页对象
            $p = new Page($count, $listRows);

            $list = $config_db->where($map)->limit($p->firstRow . ',' . $p->listRows)->select();

            $page = $p->show();
            $this->assign('list', $list);
            $this->assign('page', $page);
        }

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->display();
    }

    //新增参数
    public function add()
    {
        $config_db = M('Config');
        if (IS_POST) {
            $_POST['lang'] = LANG_ID;

            if (false === $config_db->create()) {
                $this->error($config_db->getError());
            }
            //保存当前数据对象
            $list = $config_db->add();

            saveCache('Config');
            if ($list !== false) {
                $this->success('添加成功！');
            } else {
                $this->error('添加失败！');
            }
        } else {
            $this->display();
        }
    }

    //编辑参数
    public function edit()
    {
        $config_db = M('Config');
        if (IS_POST) {

            if (false === $config_db->create()) {
                $this->error($config_db->getError());
            }
            //保存当前数据对象
            $list = $config_db->save();

            saveCache('Config');
            if ($list !== false) {
                $this->success('修改成功！');
            } else {
                $this->error('修改失败！');
            }
        } else {
            $id = I("get.id", 0, 'intval');
            if (!$id) {
                $this->error('缺少必要的参数！');
            }

            $data = $config_db->find($id);

            $this->assign('vo', $data);
            $this->display();
        }
    }

    function delete()
    {
        $config_db = M('Config');
        $id = I('get.id', 0, 'intval');

        if (isset($id)) {
            if (false !== $config_db->delete($id)) {
                $this->success('删除成功！');
            } else {
                $this->error('删除失败！: ' . $config_db->getDbError());
            }
        } else {
            $this->error('缺少必要的参数!');
        }
    }

    //排序
    public function listorder()
    {
        $config_db = M('Config');
        $ids = $_POST['listorders'];

        foreach ($ids as $key => $r) {
            $data['listorder'] = $r;
            $config_db->where('id=' . $key)->save($data);
        }

        $this->success('提交成功!');
    }
}